import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/login'
import register from '@/views/register/register'
import forget from '@/views/forget/forget'
import index from '@/views/index/index'
import food from '@/views/food/food'
import restaurant from '@/views/restaurant/restaurant'
import favor from '@/views/favor/favor'
import about from '@/views/about/about'
import center from '@/views/center/center'
import myShare from '@/views/myShare/myShare'
import addShare from '@/views/addShare/addShare'
import editShare from '@/views/editShare/editShare'
import foodInfo from '@/views/food/foodInfo'
import restaurantInfo from '@/views/restaurant/restaurantInfo'
import error404 from '@/views/error/404'

Vue.use(VueRouter)


const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/register',
    name: 'register',
    component: register
  },
  {
    path: '/forget',
    name: 'forget',
    component: forget
  },
  {
    path: '/',
    name: 'index',
    component: index
  },
  {
    path: '/food',
    name: 'food',
    component: food
  },
  {
    path: '/restaurant',
    name: 'restaurant',
    component: restaurant
  },
  {
    path: '/favor',
    name: 'favor',
    component: favor
  },
  {
    path: '/about',
    name: 'about',
    component: about
  },
  {
    path: '/center',
    name: 'center',
    component: center
  },
  {
    path: '/myShare',
    name: 'myShare',
    component: myShare
  },
  {
    path: '/addShare',
    name: 'addShare',
    component: addShare
  },
  {
    path: '/editShare',
    name: 'editShare',
    component: editShare
  },
  {
    path: '/foodInfo',
    name: 'foodInfo',
    component: foodInfo
  },
  {
    path: '/restaurantInfo',
    name: 'restaurantInfo',
    component: restaurantInfo
  },
  {
    path: '/404',
    name: '404',
    component: error404,
  },
  { 
    path: '/:pathMatch(.*)',
    redirect: '/404'
  },
]

// 防止连续点击多次路由报错
let routerPush = VueRouter.prototype.push;
let routerReplace = VueRouter.prototype.replace;
// push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'history',
  routes
})

export default router
